.TH std::swap(std::array) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::swap(std::array) \- std::swap(std::array)

.SH Synopsis
   Defined in header <array>
   template< class T, std::size_t N >
                                              \fI(since C++11)\fP
   void swap( std::array<T, N>& lhs,          \fI(until C++17)\fP

              std::array<T, N>& rhs );
   template< class T, std::size_t N >

   void swap( std::array<T, N>& lhs,          \fI(since C++17)\fP
              std::array<T, N>& rhs )         (constexpr since C++20)

                  noexcept(/* see below */);

   Specializes the std::swap algorithm for std::array. Swaps the contents of lhs and
   rhs. Calls lhs.swap(rhs).

   This overload participates in overload resolution only if N == 0 or    \fI(since C++17)\fP
   std::is_swappable_v<T> is true.

.SH Parameters

   lhs, rhs - containers whose contents to swap

.SH Return value

   \fI(none)\fP

.SH Complexity

   Linear in size of the container.

.SH Exceptions

   noexcept specification:           \fI(since C++17)\fP
   noexcept(noexcept(lhs.swap(rhs)))

.SH Example


// Run this code

 #include <algorithm>
 #include <iostream>
 #include <array>

 int main()
 {
     std::array<int, 3> alice{1, 2, 3};
     std::array<int, 3> bob{7, 8, 9};

     auto print = [](const int& n) { std::cout << ' ' << n; };

     // Print state before swap
     std::cout << "Alice:";
     std::for_each(alice.begin(), alice.end(), print);
     std::cout << "\\n" "Bob  :";
     std::for_each(bob.begin(), bob.end(), print);
     std::cout << '\\n';

     std::cout << "-- SWAP\\n";
     std::swap(alice, bob);

     // Print state after swap
     std::cout << "Alice:";
     std::for_each(alice.begin(), alice.end(), print);
     std::cout << "\\n" "Bob  :";
     std::for_each(bob.begin(), bob.end(), print);
     std::cout << '\\n';
 }

.SH Output:

 Alice: 1 2 3
 Bob  : 7 8 9
 -- SWAP
 Alice: 7 8 9
 Bob  : 1 2 3

.SH See also

   swap swaps the contents
        \fI(public member function)\fP

.SH Category:
     * conditionally noexcept
